package com.collect.tanzania.zhiScene.zhiTelemarketing.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.tanzania.MyApplication;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiTelemarketing.ZHITelemarketingFollowAdjuster;
import com.collect.tanzania.zhiAdater.zhiTelemarketing.ZHITelemarketingItemClickListener;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRTelemarketingFollowEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISTelemarketingFollowListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISTelemarketingMarkedEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectConstants;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHISysShareHelper;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiLogic.ZHITelemarketingFollowHelper;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiRoot.ZHIRootTelemaketingFragmentInterface;
import com.collect.tanzania.zhiScene.zhiWidget.zhiSubmitRecTagView.ZHISubmitRecTelemarketingView;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHITelemarketingFollowFragment extends BaseFragment<ZHITelemarketingFollowPresenter> implements ZHITelemarketingFollowContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.iv_products)
    ImageView iv_products;

    @BindView(R.id.tv_query_count)
    TextView tv_query_count;

    private ZHISubmitRecTelemarketingView mZHISubmitRecTelemarketingView;

    private ZHIRootTelemaketingFragmentInterface mZHIRootTelemaketingFragmentInterface;

    private final ZHITelemarketingFollowAdjuster mZHITelemarketingFollowAdjuster = new ZHITelemarketingFollowAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_telemarketing_follow;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            mZHISubmitRecTelemarketingView = new ZHISubmitRecTelemarketingView(getActivity());
            mZHISubmitRecTelemarketingView.setOnSubmitRecRecallItemClickListener(zhisTelemarketingMarkedEntity -> {
                if (zhisTelemarketingMarkedEntity != null && mPresenter != null) {
                    mPresenter.markTelemarketing(getActivity(), zhisTelemarketingMarkedEntity, true);
                }
            });

            iv_filter.setOnClickListener(v -> showFilter());
            iv_products.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mZHIRootTelemaketingFragmentInterface = (ZHIRootTelemaketingFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        if (ZHITelemarketingFollowHelper.FOLLOW_TYPE == ZHITelemarketingFollowHelper.FOLLOW_TYPE_WHATSAPP) {
            ZHITelemarketingFollowHelper.FOLLOW_TYPE = ZHITelemarketingFollowHelper.FOLLOW_TYPE_NONE;
            initData(true);
        }
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetTelemarketingList(ArrayList<ZHIRTelemarketingFollowEntity> zhirTelemarketingFollowEntityArrayList) {
        try {
            srl_swipe_layout.setRefreshing(false);
            mZHITelemarketingFollowAdjuster.setNewData(zhirTelemarketingFollowEntityArrayList);
            tv_page_title.setText("Follow(" + zhirTelemarketingFollowEntityArrayList.size() + ")");
            tv_query_count.setText("Total:" + zhirTelemarketingFollowEntityArrayList.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMarkTelemarketing() {
        mZHISubmitRecTelemarketingView.dismiss();
        initData(true);
    }

    @Override
    public void onMarkTelemarketingWhatsapp(final String mobile, final String whatsappContent) {
        try {
            ZHITelemarketingFollowHelper.FOLLOW_TYPE = ZHITelemarketingFollowHelper.FOLLOW_TYPE_WHATSAPP;
            ZHISysShareHelper.sendMsgWithWhatsApp(MyApplication.getAppContext(),  ZHIProjectConstants.PRE_AREA_FLAG + mobile, whatsappContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));
            mZHITelemarketingFollowAdjuster.setEmptyView(getRefreshEmptyView());
            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_list.setAdapter(mZHITelemarketingFollowAdjuster);

            mZHITelemarketingFollowAdjuster.setZHITelemarketingItemClickListener(new ZHITelemarketingItemClickListener() {
                @Override
                public void OnItemClick(ZHIRTelemarketingFollowEntity zhirTelemarketingFollowEntity) {
                    showSendSmsView(zhirTelemarketingFollowEntity);
                }

                @Override
                public void OnWhatsappFollowClick(ZHIRTelemarketingFollowEntity zhirTelemarketingFollowEntity) {
                    if (mPresenter != null) {
                        ZHISTelemarketingMarkedEntity zhisTelemarketingMarkedEntity = new ZHISTelemarketingMarkedEntity();
                        zhisTelemarketingMarkedEntity.setMobile(zhirTelemarketingFollowEntity.getMobile());
                        zhisTelemarketingMarkedEntity.setRemark("whatsapp");
                        zhisTelemarketingMarkedEntity.setFollowId(zhirTelemarketingFollowEntity.getFollowId());

                        final String gmobile = zhirTelemarketingFollowEntity.getMobile();
                        final String gshareContent = zhirTelemarketingFollowEntity.getWhatsAppContent();
                        mPresenter.markTelemarketingWhatsapp(getActivity(), zhisTelemarketingMarkedEntity, gmobile, gshareContent);
                    }
                }

                @Override
                public void OnItemFollowStateChangedListener(ZHIRTelemarketingFollowEntity zhirTelemarketingFollowEntity, int position, String followState) {
                    if (mPresenter != null) {
                        ZHISTelemarketingMarkedEntity zhisTelemarketingMarkedEntity = new ZHISTelemarketingMarkedEntity();
                        zhisTelemarketingMarkedEntity.setMobile(zhirTelemarketingFollowEntity.getMobile());
                        zhisTelemarketingMarkedEntity.setFollowStatus(followState);
                        zhisTelemarketingMarkedEntity.setFollowId(zhirTelemarketingFollowEntity.getFollowId());
                        mPresenter.markTelemarketing(getActivity(), zhisTelemarketingMarkedEntity, false);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            ZHISTelemarketingFollowListEntity zhisTelemarketingFollowListEntity = getCaseSearchParams();

            if (zhisTelemarketingFollowListEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }

            mPresenter.getTelemarketingList(getActivity(), zhisTelemarketingFollowListEntity, showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    @SuppressLint("InflateParams")
    private void showSendSmsView(ZHIRTelemarketingFollowEntity zhirTelemarketingFollowEntity) {
        try {
            if (mZHISubmitRecTelemarketingView == null) {
                mZHISubmitRecTelemarketingView = new ZHISubmitRecTelemarketingView(getActivity());
            }
            View parentView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_root_customer_service, null);
            mZHISubmitRecTelemarketingView.setName(zhirTelemarketingFollowEntity.getTargetProduct());
            mZHISubmitRecTelemarketingView.setContact(zhirTelemarketingFollowEntity.getMobile());
            mZHISubmitRecTelemarketingView.setOrderId(zhirTelemarketingFollowEntity.getFollowId());
            mZHISubmitRecTelemarketingView.setType(getCaseSearchParams().getFollowFlag());
            mZHISubmitRecTelemarketingView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshData() {
        initData(true);
    }

    public void showFilter() {
        if (mZHIRootTelemaketingFragmentInterface != null) {
            mZHIRootTelemaketingFragmentInterface.onShowFilterFollow();
        }
    }

    public ZHISTelemarketingFollowListEntity getCaseSearchParams() {
        if (mZHIRootTelemaketingFragmentInterface != null) {
            return mZHIRootTelemaketingFragmentInterface.onGetFollowFilterParams();
        }
        return new ZHISTelemarketingFollowListEntity();
    }

}
